package com.woniu.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.project.client.ProductEntityCient;
import com.woniu.project.core.domain.*;
import com.woniu.project.core.dto.ResultDto;
import com.woniu.project.core.exception.ProjectProductException;
import com.woniu.project.dao.ProductBrandMapper;
import com.woniu.project.dto.productbrand.ProductBrandSelectDto;
import com.woniu.project.params.productbrand.ProductBrandAddParam;
import com.woniu.project.params.productbrand.ProductBrandSelectParam;
import com.woniu.project.params.productbrand.ProductBrandUpdateParam;
import com.woniu.project.service.IProductBrandBigTypeService;
import com.woniu.project.service.IProductBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.project.service.IProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2020-10-12
 */
@Service
@Transactional
public class ProductBrandServiceImpl extends ServiceImpl<ProductBrandMapper, ProductBrand> implements IProductBrandService {

    @Autowired
    private IProductBrandBigTypeService productBrandBigTypeService;
    @Autowired
    private ProductEntityCient productEntityCient;
    @Autowired
    private IProductService productService;

    //根据是否有name进行分页查询
    @Override
    public IPage<ProductBrandSelectDto> select(ProductBrandSelectParam productBrandSelectParam) {
        Page<ProductBrand> page = new Page<>(productBrandSelectParam.getPage(), productBrandSelectParam.getSize());
        QueryWrapper<ProductBrand> wrapper = new QueryWrapper<ProductBrand>();
        wrapper.like(productBrandSelectParam.getName() != null && !productBrandSelectParam.getName().equals(""),
                "product_brand_name", productBrandSelectParam.getName());
        wrapper.or();
        wrapper.like(productBrandSelectParam.getLetter() != null && !productBrandSelectParam.getLetter().equals(""),
                "product_brand_letter", productBrandSelectParam.getLetter());
        if(productBrandSelectParam.getProductBigTyeId()!= null && !productBrandSelectParam.getProductBigTyeId().equals("")){
            QueryWrapper<ProductBrandBigType> wrapper11 = new QueryWrapper<ProductBrandBigType>();
            wrapper11.eq("product_big_type_id", productBrandSelectParam.getProductBigTyeId());
            List<ProductBrandBigType> list = productBrandBigTypeService.list(wrapper11);
            List<Integer> list1=new ArrayList<>();
            for (ProductBrandBigType productBrandBigType : list) {
                list1.add(productBrandBigType.getProductBrandId());
            }
            wrapper.in("product_brand_id", list1);
        }
        wrapper.orderByAsc("product_brand_sort");
        IPage ipage = page(page, wrapper);
        //entity换为dto
        List<ProductBrand> records = ipage.getRecords();
        List<ProductBrandSelectDto> productBrandSelectDtos = new ArrayList<>();
        for (int i = 0; i < records.size(); i++) {
            ProductBrandSelectDto productBrandSelectDto = new ProductBrandSelectDto();
            BeanUtils.copyProperties(records.get(i), productBrandSelectDto);
            //根据品牌id找出对应大类id
            QueryWrapper<ProductBrandBigType> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("product_brand_id", records.get(i).getProductBrandId());
            List<ProductBrandBigType> list = productBrandBigTypeService.list(wrapper1);
            List<Integer> list1 = new ArrayList<>();
            for (ProductBrandBigType productBrandBigType : list) {
                list1.add(productBrandBigType.getProductBigTypeId());
            }
            productBrandSelectDto.setProductBigTypeIds(list1);
            productBrandSelectDtos.add(productBrandSelectDto);
        }
        ipage.setRecords(productBrandSelectDtos);
        return ipage;
    }

    @Override
    public int addBrand(ProductBrandAddParam productBrandAddParam) {
        ProductBrand productBrand = new ProductBrand();
        BeanUtils.copyProperties(productBrandAddParam, productBrand);

        //查询所有品牌序号，如有序号等于当前序号则返回1
        QueryWrapper<ProductBrand> wrapper = new QueryWrapper<ProductBrand>();
        wrapper.eq("product_brand_sort", productBrand.getProductBrandSort());
        List<ProductBrand> list = list(wrapper);
        if (list.size() != 0) {
            return 1;
        }
        //品牌增加
        save(productBrand);
        //品牌大类对应表增加
        List<Integer> productBigTypeIds = productBrandAddParam.getProductBigTypeIds();
        for (Integer productBigTypeId : productBigTypeIds) {
            ProductBrandBigType productBrandBigType = new ProductBrandBigType();
            productBrandBigType.setProductBrandId(productBrand.getProductBrandId());
            productBrandBigType.setProductBigTypeId(productBigTypeId);
            productBrandBigTypeService.save(productBrandBigType);
        }
        return 0;
    }

    @Override
    public int updateBrand(ProductBrandUpdateParam productBrandUpdateParam) {
        ProductBrand productBrand = new ProductBrand();
        BeanUtils.copyProperties(productBrandUpdateParam, productBrand);

        //查询所有品牌序号，如有序号等于当前序号则返回1
        QueryWrapper<ProductBrand> wrapper = new QueryWrapper<ProductBrand>();
        wrapper.eq("product_brand_sort", productBrand.getProductBrandSort());
        wrapper.ne("product_brand_id", productBrandUpdateParam.getProductBrandId());
        List<ProductBrand> list = list(wrapper);
        if (list.size() > 0) {
            return 1;
        }
        //品牌修改
        updateById(productBrand);
        //品牌大类对应表修改
        QueryWrapper<ProductBrandBigType> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("product_brand_id", productBrand.getProductBrandId());
        productBrandBigTypeService.remove(wrapper1);
        List<Integer> productBigTypeIds = productBrandUpdateParam.getProductBigTypeIds();
        for (Integer productBigTypeId : productBigTypeIds) {
            ProductBrandBigType productBrandBigType = new ProductBrandBigType();
            productBrandBigType.setProductBrandId(productBrand.getProductBrandId());
            productBrandBigType.setProductBigTypeId(productBigTypeId);
            productBrandBigTypeService.save(productBrandBigType);
        }
        return 0;
    }

    @Override
    public void deleteById(Integer id) {
        ResultDto resultDto = productEntityCient.selectByBrandId(id);
        if (!resultDto.getData().toString().equals("[]")) {
            throw new ProjectProductException(500, "该品牌下还有商品，不能删除");
        }
        //删除大类品牌中间表
        QueryWrapper<ProductBrandBigType> wrapper1 = new QueryWrapper<ProductBrandBigType>();
        wrapper1.eq("product_brand_id", id);
        productBrandBigTypeService.remove(wrapper1);
        //删除类型
        QueryWrapper<Product> wrapper2 = new QueryWrapper<Product>();
        wrapper2.eq("product_brand_id", id);
        productService.removeById(wrapper2);
        //删除品牌
        removeById(id);
    }
}
